<script setup lang="ts">
// 系统管理-租户管理
import {onMounted, reactive, ref} from 'vue'
import {Refresh, Search} from '@element-plus/icons-vue'
import {deleteTenementApi, getTenementPageApi} from '@/assets/api/systemManagement/system/tenement'
import {ElMessage} from 'element-plus'
import TenementEdit from '@/views/systemManagement/system/tenement/TenementEdit.vue'
import TableToolButton from '@/components/base/TableToolButton.vue'

const form = reactive({
  name: '',
  simpleName: '',
  startDate: '',
  endDate: '',
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 搜索日期区间
const date = ref([])
// 搜索
const onSubmit = () => {
  form.pageNum = 1
  form.startDate = date.value[0]
  form.endDate = date.value[1]
  getTableData()
}

// 重置
const onReset = () => {
  form.name = ''
  form.simpleName = ''
  form.startDate = ''
  form.endDate = ''
  date.value = []
}

// 全选按钮
const multipleTableRef = ref()
const multipleSelection = ref<object[]>([])

const handleSelectionChange = (val: object[]) => {
  multipleSelection.value = val
}

// 获取表格数据
const tableData = ref([])
const tableLoading = ref(false)
const getTableData = () => {
  tableLoading.value = true
  getTenementPageApi(form).then(res => {
    tableLoading.value = false
    if (res.data.code == 200) {
      tableData.value = res.data.data.records
      form.total = res.data.data.total
    } else
      ElMessage.error({message: res.data.data, showClose: true})
  })
}

// 页码切换处理器
const handleCurrentChange = () => {
  getTableData()
}

// 新增、编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editForm = ref() // 编辑的数据
const openDialog = (tag: string, obj: object) => {
  dialogTitle.value = tag
  editForm.value = obj
  dialogVisible.value = true
}

// 编辑修改回调
const saveCallBack = (saved: boolean) => {
  if (saved) {
    dialogVisible.value = false
    getTableData()
  }
}

// 删除
const deleteCallBack = (ids: string) => {
  deleteTenementApi(ids).then(res => {
    if (res.data.code === 200) {
      ElMessage.success({message: res.data.data, showClose: true})
      getTableData()
    } else {
      ElMessage.error({message: res.data.data, showClose: true})
    }
  })
}

onMounted(() => {
  getTableData()
})
</script>

<template>
  <el-card shadow="never">
    <template #header>
      <el-breadcrumb>
        <el-breadcrumb-item>系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>租户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </template>
    <!-- 搜索 -->
    <el-row style="padding: 0.4%">
      <el-col :span="24">
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="租户名称">
            <el-input v-model="form.name" placeholder="请输入租户名称" style="width: 200px" clearable/>
          </el-form-item>
          <el-form-item label="租户简称">
            <el-input v-model="form.simpleName" placeholder="请输入租户简称" style="width: 200px" clearable/>
          </el-form-item>
          <el-form-item label="服务时间">
            <el-date-picker
              v-model="date"
              type="daterange"
              unlink-panels
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :icon="Search">搜索</el-button>
            <el-button @click="onReset" :icon="Refresh">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 表格工具 -->
    <el-row style="padding: 0.4%">
      <el-col :span="24">
        <table-tool-button :export-visible="false" :selected-array="multipleSelection" @to-add="openDialog"
                           @to-edit="openDialog" @to-delete="deleteCallBack"/>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row style="padding: 0.4%">
      <el-col :span="24">
        <el-table
          ref="multipleTableRef"
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          border
        >
          <el-table-column type="selection" width="55"/>
          <el-table-column property="name" label="租户名"/>
          <el-table-column property="simpleName" label="租户简称"/>
          <el-table-column property="tenantKey" label="分库标识"/>
          <el-table-column property="dbUrl" label="分库地址"/>
          <el-table-column property="serveTime" label="服务截至时间">
            <template #default="scope">
              {{ scope.row.serveTime.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column property="status" label="租户状态">
            <template #default="scope">
              <el-tag v-if="scope.row.status === 1" type="success">启动</el-tag>
              <el-tag v-else type="danger">停用</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination :page-size="form.pageSize" v-model:current-page="form.pageNum"
                       @current-change="handleCurrentChange" layout="prev, pager, next" v-model:total="form.total"/>
      </el-col>
    </el-row>
    <!--  新增、编辑弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600"
      :destroy-on-close="true"
    >
      <tenement-edit :form="editForm" :disabled="dialogTitle==='编辑'" @save="saveCallBack"
                     @close="dialogVisible = false"></tenement-edit>
    </el-dialog>
  </el-card>
</template>

<style scoped>

</style>
